using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace View.Instant
{
    //传入的数据结构体
    public struct CommonPopConfig
    {
        /// <summary>
        /// 目前这个type是类型的意思，1表示默认居中，2表示有标签 最多只支持三个
        /// </summary>
        public int type;
        public Action _funcClose;
        
        public bool _isNo;
        public string _txtNo;
        public Action _funcNo;
        
        public bool _isYes;
        public string _txtYes;
        public Action _funcYes;
        public string _imgYesPath;
        public string _imgYesName;
        
        public List<string> _contentList;
    }
    public class CommonPopView : View
    {
        private Button m_btnClose;
        private Button m_btnNo;
        private Text m_txtNo;
        private Button m_btnYes;
        private Text m_txtYes;
        private Image m_imgYes;
        
        GameObject [] m_objType = new GameObject[7];
        private Dictionary<int,List<Text>> m_txtTypeDic = new Dictionary<int,List<Text>>();

        private Text mtxtShowText3;
        // private Text m_txtType1;
        // private Text[] m_txtType2 = new Text[3];
        // private Text m_txtType3;
        
        public override void OnLoad()
        {
            m_btnClose = transform.Find("btnClose").GetComponent<Button>();
            m_btnNo = transform.Find("GameObject/btnNo").GetComponent<Button>();
            m_txtNo = transform.Find("GameObject/btnNo/txtNo").GetComponent<Text>();
            
            m_btnYes = transform.Find("GameObject/btnOk").GetComponent<Button>();
            m_txtYes = transform.Find("GameObject/btnOk/txtYes").GetComponent<Text>();
            m_imgYes = transform.Find("GameObject/btnOk/Image").GetComponent<Image>();

            for (int i = 0; i < m_objType.Length; i++)
            {
                string loadpath = "Type"+(i + 1);
                m_objType[i] = transform.Find(loadpath).gameObject;
                
                List<Text> itemlist = new List<Text>();
                if (i == 1)
                {
                    for (int k = 0; k < 2; k++)
                    {
                        itemlist.Add(transform.Find(loadpath + "/Image" + (k + 1) + "/txt").GetComponent<Text>());
                    }
                }
                else
                {
                    itemlist.Add(transform.Find(loadpath + "/txt").GetComponent<Text>());
                }
                m_txtTypeDic.Add(i,itemlist);
            }

            mtxtShowText3 = transform.Find("Type3/txt_tishi").GetComponent<Text>();

            // m_txtType1 = transform.Find("Type1/txt").GetComponent<Text>();
            // for (int i = 0; i < m_txtType2.Length; i++)
            // {
            //     m_txtType2[i] = transform.Find("Type2/Image" + (i + 1) + "/txt").GetComponent<Text>();
            // }
            // m_txtType3 = transform.Find("Type3/txt").GetComponent<Text>();
        }

        public override void OnShow(params object[] args)
        {
            CommonPopConfig config = (CommonPopConfig)args[0];

            //显示需要显示的信息
            int type = 1;
            if (config.type != null)
                type = config.type;
            if (type <= 0)
            {
                type = 1;
            }
            for (int i = 0; i < m_objType.Length; i++)
            {
                m_objType[i].gameObject.SetActive(i==(type-1));
            }
            if (type == 1)
            {
                m_txtTypeDic[0][0].text = config._contentList[0];
            }
            else if (type == 2)
            {
                for (int i = 0; i < m_txtTypeDic[1].Count; i++)
                {
                    m_txtTypeDic[1][i].transform.parent.gameObject.SetActive(false);
                }
                for (int i = 0; i < m_txtTypeDic[1].Count; i++)
                {
                    if (i <= config._contentList.Count)
                    {
                        m_txtTypeDic[1][i].text = config._contentList[i];
                        m_txtTypeDic[1][i].transform.parent.gameObject.SetActive(true);
                    }
                }
            }
            else if (type == 3)
            {
                mtxtShowText3.text = "Only VIP players can play this game.";
                if (GlobalUserItem.KindID == 145 || GlobalUserItem.KindID == 147 || GlobalUserItem.KindID == 148 || GlobalUserItem.KindID == 149 || GlobalUserItem.KindID == 151)
                {
                    mtxtShowText3.text = "<color=#C53C00>No-VIP</color> players can Bet uo to <color=#C53C00>₹ 5</color> each time";
                }
                m_txtTypeDic[2][0].text = string.Format("<color=#C53C00>Totally</color> recharge <color=#C53C00>₹{0}</color> or more become a VlP1 player.", config._contentList[0]);
                // string.Format("Add <color=#C53C00>₹{0}</color> cash now to become a VIP player", config._contentList[0]);
                // string.Format("<color=#C53C00>Totally</color> recharge <color=#C53C00>₹{0}<color> or more become a VlP player.",config._contentList[0]);
            }
            else if (type == 4)
            {
                m_txtTypeDic[3][0].text = string.Format("<color=#C53C00>Totally</color> recharge <color=#C53C00>₹{0}</color> or more become a <color=#C53C00>VlP1</color> player.", config._contentList[0]);
                // string.Format("Add <color=#C53C00>₹{0}</color> cash now to become a VIP player", config._contentList[0]);
                // string.Format("<color=#C53C00>Totally</color> recharge <color=#C53C00>₹{0}<color> or more become a VlP player.",config._contentList[0]);
            }
            else if (type == 5)
            {
                //mtxtShowText3.text = "Only VIP players can play this game.";
                //m_txtTypeDic[4][0].text = config._contentList[0];
            }
            else if (type == 6)   //web下载提示
            {
                Debug.Log("666666666666666666666666666u");
                m_txtTypeDic[5][0].text = config._contentList[0];
                RectTransform rectTransform = m_btnYes.transform as RectTransform;
                rectTransform.sizeDelta = new Vector2(417.3f, 145.6f);
                m_txtYes.transform.localPosition = new Vector3(-10f, 0, 0);
            }
            else if (type == 7)   //web下载提示
            {
                Debug.Log("666666666666666666666666666u");
                m_txtTypeDic[6][0].text = config._contentList[0];
            }

            ///设置按钮状态和显示信息
            m_btnNo.gameObject.SetActive(config._isNo);
            m_btnYes.gameObject.SetActive(config._isYes);
            
            m_txtNo.text = "";
            m_imgYes.gameObject.SetActive(false);
            m_txtYes.gameObject.SetActive(false);
            
            //检测取消按钮文字
            if (string.IsNullOrEmpty(config._txtNo)==false)
                m_txtNo.text = config._txtNo;
            else
                m_txtNo.text = "No";
            
            //检测确定按钮文字
            bool ishavetxtorimg = false;
            if (string.IsNullOrEmpty(config._imgYesPath)==false && string.IsNullOrEmpty(config._imgYesName)==false)
            {
                Sprite sp = TexturePackerManager.LoadSprite(config._imgYesPath, config._imgYesName);
                if (sp != null)
                {
                    m_imgYes.sprite = sp;
                    m_imgYes.SetNativeSize();
                    m_imgYes.gameObject.SetActive(true);
                    ishavetxtorimg = true;
                }
            }
            else if (string.IsNullOrEmpty(config._txtYes)==false)
            {
                m_txtYes.text = config._txtYes;
                m_txtYes.gameObject.SetActive(true);
                ishavetxtorimg = true;
            }

            if (ishavetxtorimg==false)
            {
                m_txtYes.gameObject.SetActive(true);
                m_txtYes.text = "Yes";
            }

            RemoveClickListener(m_btnClose);
            AddClickListener(m_btnClose, () =>
            {
                if (config._funcClose != null)
                {
                    config._funcClose();
                    config._funcClose = null;
                }
                if (type == 6)
                {
                    RectTransform rectTransform1 = m_btnYes.transform as RectTransform;
                    rectTransform1.sizeDelta = new Vector2(320, 112);
                    m_txtYes.transform.localPosition = new Vector3(0, 0, 0);
                }
                // UIManager.Instance.Close(ViewName.CommonPopView);
            });
            
            RemoveClickListener(m_btnNo);
            AddClickListener(m_btnNo, () =>
            {
                if (config._funcNo != null)
                {
                    config._funcNo();
                    config._funcNo = null;
                }
                // UIManager.Instance.Close(ViewName.CommonPopView);
            });
            
            RemoveClickListener(m_btnYes);
            AddClickListener(m_btnYes, () =>
            {
                if (config._funcYes != null)
                {
                    config._funcYes();
                    config._funcYes = null;
                }
                // UIManager.Instance.Close(ViewName.CommonPopView);
            });
        }

        void Oninit()
        {
        }

        void OnEvent()
        {
            
        }
    }

}
